﻿using System;
using System.Data;
using System.Web.UI;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Configuration.Settings;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.UI.DataTypes;
using ASPNETVN.PORTAL.Modules.FileExpress.Components;

namespace ASPNETVN.PORTAL.Modules.FileExpress
{
    public enum ModulePages
    {
        Package,
        Attachment,
    }

    public partial class Main : PortalModuleControl
    {
        public Main()
        {
            ModuleSettingItem uploadPath = new ModuleSettingItem(new StringDataType("Uploads"));
            uploadPath.Name = "Thư mục upload";
            uploadPath.Description = "Thư mục upload";
            uploadPath.Order = 0;
            this.BaseSettings.Add("UploadPath", uploadPath);
            /*
            SettingItem deviceIP = new SettingItem(new BooleanDataType(), "True");
            deviceIP.Name = "Device IP";
            deviceIP.Order = 0;
            this.BaseSettings.Add("DeviceIP", deviceIP);

            SettingItem port = new SettingItem(new StringDataType(), "4730");
            port.Name = "Port";
            port.Order = 1;
            this.BaseSettings.Add("Port", port);

            SettingItem list = new SettingItem(new ListDataType("1;2;3;4;5"), "1");
            list.Name = "List";
            list.Order = 2;
            this.BaseSettings.Add("List", list);


            ArrayList array = new ArrayList();
            array.Add(new ListItem("IBM", "1"));
            array.Add(new ListItem("Microsoft", "2"));
            array.Add(new ListItem("SUN", "3"));
            array.Add(new ListItem("Oracle", "4"));

            SettingItem cbl = new SettingItem(new CheckBoxListDataType(array, "Text", "Value"), "1;4");
            cbl.Name = "CheckBoxList";
            cbl.Order = 3;
            cbl.Group = SettingItemGroup.Meta;
            this.BaseSettings.Add("CheckBoxList", cbl);
            */
        }

        private void loadChildControl()
        {
            const string basePath = "~/Modules/FileExpress/";
            string pageQuery = "Page" + this.ModuleID;
            string pageName = this.Request.QueryString[pageQuery];

            ModulePages page;
            if (pageName == null)
            {
                page = ModulePages.Package;
            }
            else
            {
                page = (ModulePages) Enum.Parse(typeof (ModulePages), this.Request.QueryString[pageQuery], true);
            }

            string src = string.Format("{0}Controls/{1}.ascx", basePath, page);
            if (src.IndexOf("/Mod_") >= 0)
                src = src.Replace("/Mod_", "/Mod/");
            if (src.IndexOf("/Admin_") >= 0)
                src = src.Replace("/Admin_", "/Admin/");
            if (src.IndexOf("/Help_") >= 0)
                src = src.Replace("/Help_", "/Help/");

            ModulePage ctl = (ModulePage)this.LoadControl(src);
            ctl.ParentControl = this;
            this.ContentPane.Controls.Add(ctl);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.loadChildControl();
            if (!this.IsPostBack)
            {
                this.ModuleTitle.AddUrl = string.Format("~/AddEditPage.aspx?tabindex={0}&tabid={1}&itemid={2}&mid={3}&page=AddEditPackage", this.TabIndex, this.TabID, 0, this.ModuleID);
            }

            LiteralControl literal = new LiteralControl("");

            literal.Text += "<table border='0' cellpadding='4' cellspacing='0' style='width: 100%'>";

            string url = HttpUrlBuilder.BuildUrl(this.ModuleID, 0, ModulePages.Package);
            url = url.Substring(2);
            literal.Text += string.Format("<tr><td width='100%'><a href='{0}' class='VerticalMenuItem'>» Trang nhất</a></td></tr>", url);
            literal.Text += "</table>";

            // Manager Permission.
            /*
            if (PortalSecurity.HasManagerPermission(this.ModuleID))
            {
                literal.Text += "<table border='0' cellpadding='6' cellspacing='0' style='width: 100%'><tr><td class='MainMenuGroup' width='100%'>Management</td></tr></table>";
                literal.Text += "<table border='0' cellpadding='4' cellspacing='0' style='width: 100%'>";
                literal.Text += string.Format("<tr><td width='100%'><a href='{0}' class='MainMenuItem'>» Item</a></td></tr>", YAFUrlBuilder.GetLink(true, this.ModuleID, ModulePages.ItemManager));
                literal.Text += string.Format("<tr><td width='100%'><a href='{0}' class='MainMenuItem'>» Category</a></td></tr>", YAFUrlBuilder.GetLink(true, this.ModuleID, ModulePages.CategoryManager));
                literal.Text += string.Format("<tr><td width='100%'><a href='{0}' class='MainMenuItem'>» Flow</a></td></tr>", YAFUrlBuilder.GetLink(true, this.ModuleID, ModulePages.FlowManager));
                literal.Text += "</table>";
            }
            */
            DataSet ds = Category.SelectAll(this.ModuleID);
            DataRow[] rootRows = ds.Tables[0].Select("ParentID = 0");

            foreach (DataRow row in rootRows)
            {
                literal.Text += "<table border='0' cellpadding='0' cellspacing='0' style='width: 100%'><tr><td class='VerticalMenuGroup' width='100%'>" + row["Name"] + "</td></tr></table>";
                DataRow[] childRows = ds.Tables[0].Select("ParentID = " + row["ID"]);
                if (childRows.Length > 0)
                {
                    literal.Text += "<table border='0' cellpadding='4' cellspacing='0' style='width: 100%'>";
                    foreach (DataRow childRow in childRows)
                    {
                        url = HttpUrlBuilder.BuildUrl(this.ModuleID, 0, ModulePages.Package, "categoryid={0}&categoryname={1}", childRow["ID"], this.Server.UrlEncode(childRow["Name"].ToString()));
                        url = url.Substring(2);
                        literal.Text += string.Format("<tr><td width='100%'><a href='{0}' class='VerticalMenuItem'>» {1}</a></td></tr>", url, childRow["Name"]);
                    }
                    literal.Text += "</table>";
                }
            }

            this.LeftPane.Controls.Add(literal);
        }
    }
}